﻿using System;
using System.Collections.Generic;
using System.Linq;
using JStore.Core.Entities;
using SpeadFrm.ORM;

namespace JStore.Data.Daos
{
    public class WeekMoneyLogDao : BaseDao<WeekMoneyLog>
    {
        public List<WeekMoneyLog> GetAll()
        {
            var sql = Sql.Builder.Where("1=1");
            return Query(sql).ToList();
        }

        public List<WeekMoneyLog> GetList(int? weekMoneyLogId)
        {
            var sql = Sql.Builder.Where("1=1");
            if (weekMoneyLogId != null)
                sql.Where("Id = @0", weekMoneyLogId);
            sql.OrderBy("StartDate DESC");
            return Query(sql).ToList();
        }

        public bool Exists(DateTime startDate, DateTime endDate)
        {
            var sql = Sql.Builder
                .Append("SELECT COUNT(1)")
                .Append("FROM WeekMoneyLog")
                .Where("DATEDIFF(DAY,StartDate,@0)=0 AND DATEDIFF(DAY,EndDate,@1)=0", startDate, endDate);
            return FirstOrDefault<int>(sql) > 0;
        }
    }
}